package handler.assetFormHandlers;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;


import javax.swing.JOptionPane;
import checker.AssetFormChecker;
import form.AssetForm.AssetForm;

public abstract class AbstractAFErrorCheckingButtonListener implements ActionListener {
	//This class is a template for the add and edit button listeners in an AssetForm
	
	protected AssetForm assetForm;
	private AssetFormChecker inputChecker;
	
	protected AbstractAFErrorCheckingButtonListener(AssetForm assetForm, AssetFormChecker inputChecker){
		this.assetForm = assetForm;
		this.inputChecker = inputChecker;
	}

	public void actionPerformed(ActionEvent e) {
		String businessRulesErrorMsg = "";
		
		if(inputChecker!= null)
			businessRulesErrorMsg = inputChecker.getErrorMsg(assetForm);
		
		assetForm.triggerAllInputUIErrorIfAny();
		String formatErrorMsg = assetForm.getInputFormatErrorMsg();
		
		if((inputChecker==null || businessRulesErrorMsg.equals("")) && formatErrorMsg.equals("") ){
			performOperation();
		}
		else
			JOptionPane.showMessageDialog(null, formatErrorMsg+"\n"+businessRulesErrorMsg, "Error!", JOptionPane.WARNING_MESSAGE);
	}
	
	public abstract void performOperation();
	
}
